package com.huanxing.cloud.pay.api.controller.alipay;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.pay.api.config.AliPayAutoConfiguration;
import com.huanxing.cloud.pay.common.constants.PayConstants;
import com.huanxing.cloud.pay.common.dto.AliPayRefundRequestDTO;
import com.huanxing.cloud.pay.common.dto.AliPayRequestDTO;
import com.huanxing.cloud.security.annotation.HxInner;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/alipay")
@Api(value = "alipay", tags = "支付宝支付")
public class AliPayController {

	@HxInner
	@ApiOperation(value = "调用统一下单接口")
	@PostMapping("/create/order")
	public Result pay(@RequestBody AliPayRequestDTO aliPayRequestDTO) throws Exception {
		AlipayClient alipayClient = AliPayAutoConfiguration.getAlipayClient();
		JSONObject bizContent = new JSONObject();
		/****** 必传参数 ******/
		bizContent.set("out_trade_no", aliPayRequestDTO.getOutTradeNo());
		bizContent.set("total_amount", aliPayRequestDTO.getTotalAmount());
		bizContent.set("subject", aliPayRequestDTO.getSubject());
		switch (aliPayRequestDTO.getTradeType()) {
			// APP 支付
			case PayConstants.TRADE_TYPE_APP:
				AlipayTradeAppPayRequest alipayTradeAppPayRequest = new AlipayTradeAppPayRequest();
				alipayTradeAppPayRequest.setNotifyUrl(aliPayRequestDTO.getNotifyUrl());

				alipayTradeAppPayRequest.setBizContent(bizContent.toString());
				try {
					AlipayTradeAppPayResponse alipayTradeAppPayResponse = alipayClient
						.sdkExecute(alipayTradeAppPayRequest);
					if (!alipayTradeAppPayResponse.isSuccess()) {
						return Result.fail(alipayTradeAppPayResponse.getMsg() + alipayTradeAppPayResponse.getSubMsg());
					}
					return Result.success(alipayTradeAppPayResponse);
				}
				catch (AlipayApiException e) {
					throw new RuntimeException(e);
				}

				// 手机网页支付
			case PayConstants.TRADE_TYPE_H5:
				AlipayTradeWapPayRequest alipayTradeWapPayRequest = new AlipayTradeWapPayRequest();
				// 异步接收地址，仅支持http/https，公网可访问
				alipayTradeWapPayRequest.setNotifyUrl(aliPayRequestDTO.getNotifyUrl());
				// 同步跳转地址，仅支持http/https
				alipayTradeWapPayRequest.setReturnUrl(aliPayRequestDTO.getReturnUrl());
				// 用户付款中途退出返回商户网站的地址
				bizContent.set("quit_url", aliPayRequestDTO.getQuitUrl());
				alipayTradeWapPayRequest.setBizContent(bizContent.toString());
				AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.sdkExecute(alipayTradeWapPayRequest);
				if (!alipayTradeWapPayResponse.isSuccess()) {
					return Result.fail(alipayTradeWapPayResponse.getMsg() + alipayTradeWapPayResponse.getSubMsg());
				}
				return Result.success(alipayTradeWapPayResponse);
			// 小程序支付
			case PayConstants.TRADE_TYPE_JSAPI:
				AlipayTradeCreateRequest alipayTradeCreateRequest = new AlipayTradeCreateRequest();
				alipayTradeCreateRequest.setNotifyUrl(aliPayRequestDTO.getNotifyUrl());
				bizContent.set("buyer_id", aliPayRequestDTO.getBuyerId());
				alipayTradeCreateRequest.setBizContent(bizContent.toString());
				AlipayTradeCreateResponse alipayTradeCreateResponse = alipayClient
					.certificateExecute(alipayTradeCreateRequest);
				if (!alipayTradeCreateResponse.isSuccess()) {
					return Result.fail(alipayTradeCreateResponse.getMsg() + alipayTradeCreateResponse.getSubMsg());
				}
				return Result.success(alipayTradeCreateResponse);
			default:
				return Result.fail("类型错误");
		}
	}

	@HxInner
	@ApiOperation(value = "支付宝支付退款")
	@PostMapping("/refund")
	public Result refund(@RequestBody AliPayRefundRequestDTO aliPayRefundRequestDTO) throws Exception {
		AlipayClient alipayClient = AliPayAutoConfiguration.getAlipayClient();
		AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
		JSONObject bizContent = new JSONObject();
		bizContent.set("refund_amount", aliPayRefundRequestDTO.getRefundAmount());
		bizContent.set("out_trade_no", aliPayRefundRequestDTO.getOutTradeNo());
		bizContent.set("out_request_no", aliPayRefundRequestDTO.getOutRequestNo());
		request.setBizContent(bizContent.toString());

		AlipayTradeRefundResponse response = alipayClient.certificateExecute(request);
		if (!response.isSuccess()) {
			return Result.fail(response.getMsg() + response.getSubMsg());
		}
		return Result.success(response);
	}

	@HxInner
	@ApiOperation(value = "支付宝退款订单查询")
	@PostMapping("/refund/query")
	public Result refundQuery(@RequestBody AliPayRefundRequestDTO aliPayRefundRequestDTO) throws Exception {
		AlipayClient alipayClient = AliPayAutoConfiguration.getAlipayClient();
		AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
		JSONObject bizContent = new JSONObject();
		bizContent.set("out_trade_no", aliPayRefundRequestDTO.getOutTradeNo());
		bizContent.set("out_request_no", aliPayRefundRequestDTO.getOutRequestNo());
		request.setBizContent(bizContent.toString());

		AlipayTradeFastpayRefundQueryResponse response = alipayClient.certificateExecute(request);
		if (!response.isSuccess()) {
			return Result.fail(response.getMsg() + response.getSubMsg());
		}
		return Result.success(response);
	}

}
